<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click='showAddCateDialog'>新增分类</el-button>
        </el-col>
      </el-row>

      <!-- 列表 -->
      <tree-table
        :data="catelist"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
        :show-row-hover="false"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="!scope.row.cat_deleted"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>

        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level == 0">一级标题</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level == 1"
            >二级标题</el-tag
          >
          <el-tag type="warning" v-else>三级级标题</el-tag>
        </template>

        <!-- 操作 -->
        <template slot="operate">
          <el-button type="primary" size="mini" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button type="danger" size="mini" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 3, 4]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 新增分类对话框 -->
    <el-dialog
      title="新增分类"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addCateDialogClose"
    >
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
            <el-cascader
            v-model="selectedKeys"
            clearable
            :options="parentCateList"
            :props="{ expandTrigger: 'hover',value:'cat_id',label:'cat_name',checkStrictly: true }"
            @change="handleCascaderChange"></el-cascader>

        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      queryInfo: {
        // 页码
        pagenum: 1,
        // 每页显示多少条
        pagesize: 5
      },
      // 总条数
      total: 0,
      //   存放的数据
      catelist: [],
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
          // width: '400px'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operate'
        }
      ],
      addDialogVisible: false,
      addCateForm: {
        cat_pid: undefined,
        cat_name: '',
        cat_level: 0
      },
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentCateList: [],
      selectedKeys: []
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // 刷新数据
    async getCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      this.total = res.data.total
      this.catelist = res.data.result
    },
    // 监听pagesize
    handleSizeChange (size) {
      this.queryInfo.pagesize = size
      this.getCateList()
    },
    // 监听页码值的改变
    handleCurrentChange (num) {
      this.queryInfo.pagenum = num
      this.getCateList()
    },
    addCate () {
      this.$refs.addCateFormRef.validate(async (valid) => {
        // console.log(valid)
        if (valid) {
          // 验证通过
          const { data: res } = await this.$http.post(
            'categories',
            this.addCateForm
          )
          if (res.meta.status !== 201) {
            return this.$message({
              message: '添加分类失败',
              type: 'error',
              showClose: true,
              duration: 3000
            })
          }
          this.$message({
            message: '添加分类成功',
            type: 'success',
            showClose: true,
            duration: 3000
          })
          this.addDialogVisible = false
          this.getCateList()
        }
      })
    },
    async showAddCateDialog () {
      this.addDialogVisible = true
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      //   console.log(res)
      this.parentCateList = res.data
    },
    handleCascaderChange () {
    //   console.log(this.selectedKeys)
      if (this.selectedKeys > 0) {
        this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        this.addCateForm.cat_level = this.selectedKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    addCateDialogClose () {
      this.$refs.addCateFormRef.resetFields()
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
      this.selectedKeys = []
    }

  }
}
</script>
<style lang="less" scoped>

</style>
